from fastapi import APIRouter, Response

from app.database.postgre import SessionDep
from app.models.cookies import CookieDep
from app.models.accounts import AccountLoginJSON, ModifyPasswordJSON
from app.services import accounts

router = APIRouter(
    prefix='/api/v1/accounts',
    tags=['backend', 'accounts']
)


@router.post('/login')
async def login(json_data: AccountLoginJSON, session: SessionDep, response: Response):
    account = await accounts.handle_client_login(json_data, session)
    response = await accounts.handle_login_success(account, response)
    return response


@router.get('/me')
async def my_info(account: accounts.LoggedInAccount):
    response = await accounts.get_my_info(account)
    return response


@router.post('/logout')
async def logout(account: accounts.LoggedInAccount, cookie: CookieDep, response: Response):
    response = await accounts.handle_client_logout(account, cookie, response)
    return response


@router.post('/modify_password')
async def modify_password(account: accounts.LoggedInAccount, json_data: ModifyPasswordJSON, session: SessionDep):
    response = await accounts.modify_password_by_old(account, json_data, session)
    return response


@router.get('/simple_apps')
async def simple_apps(account: accounts.LoggedInAccount, session: SessionDep):
    response = await accounts.get_my_simple_apps(account, session)
    return response
